﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class RewardEdit : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlCommand cmdSearch;
                SqlDataReader dtrSearch;
                conn.Open();

                cmdSearch = new SqlCommand("SELECT * FROM Reward WHERE r_id=@id", conn);
                cmdSearch.Parameters.AddWithValue("@id", Request.QueryString["id"]);
                //cmdSearch.Parameters.AddWithValue("@id", 1);
                dtrSearch = cmdSearch.ExecuteReader();

                dtrSearch.Read();

                txtName.Text = dtrSearch["r_name"].ToString();
                txtDesc.Text = dtrSearch["r_desc"].ToString();
                txtPoints.Text = dtrSearch["r_points"].ToString();
                txtQuan.Text = dtrSearch["r_quantity"].ToString();

                dtrSearch.Close();
                conn.Close();
            }
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            String saveDir = @"\uploads\reward\";
            string appPath = Request.PhysicalApplicationPath;

            if (FileUpload1.HasFile)
            {
                string savePath = appPath + saveDir + Server.HtmlEncode(FileUpload1.FileName);
                String fileName = FileUpload1.FileName;

                FileUpload1.SaveAs(savePath);

                SqlCommand cmdIn;
                conn.Open();

                cmdIn = new SqlCommand("UPDATE Reward SET r_picture=@p WHERE r_id=@id", conn);
                cmdIn.Parameters.AddWithValue("@p", fileName);
                cmdIn.Parameters.AddWithValue("@id", Request.QueryString["id"]);
                cmdIn.ExecuteNonQuery();

                conn.Close();
                MessageBox("Your file was saved as " + fileName + ".");
                Redirect("RewardEdit.aspx?id=" + Request.QueryString["id"]);
            }
            else
            {
                MessageBox("You did not specify a file to upload.");
            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SqlCommand cmdIn;
            conn.Open();

            cmdIn = new SqlCommand("UPDATE Reward SET r_name=@name, r_desc=@desc, r_points=@points, r_quantity=@quan WHERE r_id=@id", conn);
            cmdIn.Parameters.AddWithValue("@name", txtName.Text);
            cmdIn.Parameters.AddWithValue("@desc", txtDesc.Text);
            cmdIn.Parameters.AddWithValue("@points", txtPoints.Text);
            cmdIn.Parameters.AddWithValue("@quan", txtQuan.Text);
            cmdIn.Parameters.AddWithValue("@id", 1);
            cmdIn.ExecuteNonQuery();

            conn.Close();
            MessageBox("Update Successfully.");
        }

        public string getPic()
        {
            SqlCommand cmdSearch;
            SqlDataReader dtrSearch;
            conn.Open();

            cmdSearch = new SqlCommand("Select r_picture From Reward Where r_id=@id", conn);
            cmdSearch.Parameters.AddWithValue("@id", Request.QueryString["id"]);
            //cmdSearch.Parameters.AddWithValue("@id", 1);
            dtrSearch = cmdSearch.ExecuteReader();

            dtrSearch.Read();
            String pic = dtrSearch["r_picture"].ToString();

            conn.Close();

            return pic;

        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Redirect("RewardEdit.aspx");
        }
    }
}